<?php
namespace app\common\model;


class CourseFile extends Base
{
    /**
     * 字典
     */
    const DICT = [
        'file_type' => [
            '1' => 'WPS文档',
            '2' => 'PDF文档',
            '3' => 'MP4文件'
        ]
    ];

    /**
     * add
     * @param $uid
     * @param $type
     * @param $score
     * @param $source
     * @param $remark
     * @return integer|string
     */
    public function add($uid, $type, $score, $source = 0, $remark = '')
    {
        $data = [
            'uid' => $uid,
            'type' => $type,
            'score' => $score,
            'source' => $source,
            'remark' => $remark,
            'add_time' => time(),
        ];
        return $this->insert($data);
    }

    /**
     * 查询用户当天类型积分
     * @param int $uid
     * @param int $type
     * @return float
     */
    public function getTodayCountByUid(int $uid, int $type): float
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $where = [
            ['uid', '=', $uid],
            ['type', '=', $type],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        return $this->where($where)->sum('score');
    }

    /**
     * 查询信息
     * @param int $uid
     * @return array
     * @throws
     */
    public function getListByUid(int $uid): array
    {
        //查询数据
        $where = [['uid', '=', $uid]];
        $list = $this->where($where)->limit(5)->order('id', 'desc')->select();

        //处理数据
        $dict = self::DICT;
        foreach ($list as $key => $val)
        {
            if ($val['type'] == 2)
            {
                $list[$key]['score'] = "-{$val['score']}";
                $list[$key]['source_name'] = "题库消费";
            }
            else
            {
                $list[$key]['score'] = "+{$val['score']}";
                $list[$key]['source_name'] = $dict['source'][$val['source']] ?? '-';
            }
            $list[$key]['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
        }

        return $list ? $list->toArray() : [];
    }

    /**
     * 检查当天签到
     * @param $userId
     * @return bool
     */
    public function checkTodaySign($userId): bool
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $where = [
            ['uid', '=', $userId],
            ['source', 'in', [6, 7]],
            ['type', '=', 1],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        return (bool)$this->where($where)->count();
    }

    /**
     * 检查当天是否看视频
     * @param $userId
     * @return bool
     */
    public function checkTodayReward($userId): bool
    {
        $s_time = mktime(0, 0, 0);
        $e_time = strtotime('+1 day', $s_time);
        $userScoreRecord = new UserScoreRecord();
        $where = [
            ['uid', '=', $userId],
            ['source', '=', 8],
            ['type', '=', 1],
            ['add_time', 'between', [$s_time, $e_time]],
        ];
        $count = $userScoreRecord->where($where)->count();
        if ($count >= 3)
        {
            return false;
        }
        return true;
    }

    /**
     * 检查昨天签到
     * @param $userId
     * @return bool
     */
    public function checkYesTodaySign($userId): bool
    {
        $y_s_time = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        $y_e_time = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        $where = [
            ['uid', '=', $userId],
            ['source', '=', 6],
            ['type', '=', 1],
            ['add_time', 'between', [$y_s_time, $y_e_time]],
        ];
        return (bool)$this->where($where)->count();
    }
}